package cn.wyu.web;
import cn.wyu.pojo.Article;
import cn.wyu.service.ArticleService;
import cn.wyu.service.ArticleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import  javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/IndexServlet")
public class myServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type_code");
        System.out.println("前端发送的索引"+type);
        ArticleService as = new ArticleServiceImpl();
        List<Article> list  = as.findArticleType(type);
        //打印测试
        list.forEach(article -> {
//            System.out.println(article.getPrice());
//            System.out.println(article.getTitle());
            System.out.println("折扣:"+article.getDiscount());
        });
        //保存查询到的数据到request
        req.setAttribute("articlelist", list);

        String articleType = as.findArticle(type);
        req.setAttribute("title",articleType);
        //跳到首页，转发
        req.getRequestDispatcher("homepage.jsp").forward(req,resp);

        //TODO 不太清楚这两个方法
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
